<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, Address, Riderlist, Order, RiderStreet};
use think\Facade\Config as Configs;
use app\facade\Mns;
use think\Db;
use app\facade\Redis;
use EasyWeChat\Factory;

class Message extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Message')->setDescription('通知');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/jx/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        (new Order())->where(['status' => 1, 'message' => 1])->chunk(1, function ($list) {
            foreach ($list as $lists) {
                //判断当前区域是否有家政
                $RiderStreet = (new RiderStreet())->where(['street' => $lists['street'], 'status' => 2])->column('riderid');
                if (count($RiderStreet) > 0) {
                    $RiderList = (new RiderList())->where(['id' => $RiderStreet, 'status' => 1])->select();
                    foreach ($RiderList as $rists) {
                        if (empty($rists['openid'])) {
                            (new Mns())->sendmessage($rists['mobile'], $lists['ordersn'], $lists['name'], $lists['mobile'], $lists['address'], $lists['appointment']);
                        } else {
                            $this->wechattest($rists['openid'], '您所属区域有一个新的订单，请速去接单。',$lists['appointment'], '姓名:'.$lists['name'].' 联系电话:' . $lists['mobile'].' 地址:'.$lists['address']);
                        }
                    }
                }
                (new Order())->where(['id' => $lists['id'], 'message' => 1])->update(['message' => 2]);
            }
        });
    }


    public function wechattest($touser = '', $data1 = '', $data2 = '', $data3 = '')
    {
        $config = config('wechat2.');
        $app = Factory::officialAccount($config);
        $send_data = [
            'template_id' => 'ViH2c0i9dXCnW-WJCck_WYlS0xdwPnlJcAzpaHXtv7U', // 所需下发的订阅模板id
            'touser' => $touser,     // 接收者（用户）的 openid
            'miniprogram' => [
                'appid' => 'wx0b4e1920b3603873',
                'pagepath' => '/pages/member/horseman/Index',
            ],
            'data' => [
                'first' => [
                    'value' => $data1
                ],
                'keyword1' => [
                    'value' => $data2
                ],
                'keyword2' => [
                    'value' => $data3
                ],
                'remark' => [
                    'value' => '请尽快接单!'
                ],
            ],
        ];
        $rm = $app->template_message->send($send_data);
        dump($rm);
    }
}
